import socket, random, time, threading

class TalkClient:
    def __init__(self):
        self.client = socket.socket() # 实例化socket对象

    def connect_server(self):
        # 1. 建立与服务器端的连接
        # 2. 发送数据
        # 3. 关闭连接

        self.client.connect(('192.168.49.1', 8000))
        # print("你好，欢迎加入蜗牛聊天室，已经成功连接服务器.")

        threading.Thread(target=self.send).start()
        threading.Thread(target=self.receive).start()

    def send(self):
        while True:
            to_msg = input("")
            self.client.send(to_msg.encode())    # 将一个字符串以特定的编码格式进行编码，默认是utf-8

    def receive(self):
        while True:
            from_msg = self.client.recv(1024).decode()  # 接收来自服务器端的消息
            print(from_msg)


    def __del__(self):
        self.client.close()

if __name__ == '__main__':
    tc = TalkClient()
    tc.connect_server()
